/**
  ******************************************************************************
  * @file    sym32l010_hal_flash.h
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   Header file of FLASH HAL module.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYM32L010_HAL_FLASH_H
#define __SYM32L010_HAL_FLASH_H

#ifdef __cplusplus
extern "C" {
#endif


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_hal_def.h"


/******************************************************************************/
/* Global type definitions                                                    */
/******************************************************************************/
/* Flash Mode ----------------------------------------------------------------*/
typedef enum
{
    FLASH_MODE_READ      = 0UL,
    FLASH_MODE_PROGRAM   = 1UL,
    FLASH_MODE_PAGEERASE = 2UL,
    FLASH_MODE_CHIPERASE = 3UL
} FLASH_ModeTypeDef;

/******************************************************************************/
/* Global pre-processor symbols/macros ('#define')                            */
/******************************************************************************/
/* FLASH 参数 ----------------------------------------------------------------*/
#define FLASH_SIZE                      (*(volatile uint32_t *)(CHIPINFO_FLASHSIZE_ADDR))
#define FLASH_STARTADDR                 (0x0UL)
#define FLASH_ENDADDR                   (FLASH_STARTADDR + FLASH_SIZE - 1)

#define FLASH_PAGE_SIZE                 (512UL)                       // PAGE大小512 Byte
#define FLASH_PAGE_CNT                  (FLASH_SIZE / FLASH_PAGE_SIZE)// PAGE数量
#define FLASH_PAGE_CNT_PER_LOCK         (8UL)                         // PAGELOCK每个bit配置PAGE数量

/* FLASH 中断触发源 ----------------------------------------------------------*/
#define FLASH_IT_SOURCE_PC              (FLASH_IER_PC_Msk)
#define FLASH_IT_SOURCE_LOCK            (FLASH_IER_LOCK_Msk)
#define FLASH_IT_SOURCE_PROG            (FLASH_IER_PROG_Msk)

/* FLASH 中断触发标志 --------------------------------------------------------*/
#define FLASH_IT_FLAG_PC                (FLASH_ISR_PC_Msk)
#define FLASH_IT_FLAG_LOCK              (FLASH_ISR_LOCK_Msk)
#define FLASH_IT_FLAG_PROG              (FLASH_ISR_PROG_Msk)

/* OTP  地址范围 -------------------------------------------------------------*/
#define OTP_STARTADDR                   (0x00100760UL)
#define OTP_ENDADDR                     (0x00100775UL)

/******************************************************************************/
/* Global macro function                                                      */
/******************************************************************************/
/**
  * @brief  获取 FLASH中断触发源
  * @param  ITSOURCE: FLASH中断触发源
  *                   @arg FLASH_IT_SOURCE_PC
  *                   @arg FLASH_IT_SOURCE_LOCK
  *                   @arg FLASH_IT_SOURCE_PROG
  * @retval
  */
#define HAL_FLASH_GET_IT_SOURCE(ITSOURCE)  (SYM_FLASH->IER & (ITSOURCE))

/**
  * @brief  使能 FLASH中断触发源
  * @param  ITSOURCE: FLASH中断触发源
  *                   @arg FLASH_IT_SOURCE_PC
  *                   @arg FLASH_IT_SOURCE_LOCK
  *                   @arg FLASH_IT_SOURCE_PROG
  * @retval
  */
#define HAL_FLASH_ENABLE_IT(ITSOURCE)  (SYM_FLASH->IER |= (ITSOURCE))

/**
  * @brief  关闭 FLASH中断触发源
  * @param  ITSOURCE: FLASH中断触发源
  *                   @arg FLASH_IT_SOURCE_PC
  *                   @arg FLASH_IT_SOURCE_LOCK
  *                   @arg FLASH_IT_SOURCE_PROG
  * @retval
  */
#define HAL_FLASH_DISABLE_IT(ITSOURCE)  (SYM_FLASH->IER &= ~((uint32_t)(ITSOURCE)))

/**
  * @brief  获取 FLASH中断触发标志
  * @param  FLAG: FLASH中断触发标志
  *               @arg FLASH_IT_FLAG_PC
  *               @arg FLASH_IT_FLAG_LOCK
  *               @arg FLASH_IT_FLAG_PROG
  * @retval
  */
#define HAL_FLASH_GET_FLAG(FLAG)  (SYM_FLASH->ISR & (FLAG))

/**
  * @brief  清除 FLASH中断触发标志
  * @param  FLAG: FLASH中断触发标志
  *               @arg FLASH_IT_FLAG_PC
  *               @arg FLASH_IT_FLAG_LOCK
  *               @arg FLASH_IT_FLAG_PROG
  * @retval
  */
#define HAL_FLASH_CLR_FLAG(FLAG)  (SYM_FLASH->ICR = ~((uint32_t)(FLAG)))


/******************************************************************************/
/* Exported variables ('extern', definition in C source)                      */
/******************************************************************************/


/******************************************************************************/
/* Global function prototypes ('extern', definition in C source)              */
/******************************************************************************/
void              HAL_FLASH_ConfigWaitCycle(uint32_t HclkFreq);
uint8_t           HAL_FLASH_GetReadOutLevel(void);
void              HAL_FLASH_SetReadOutLevel(uint8_t RdLevel);
void              HAL_FLASH_LockPages(void);
HAL_StatusTypeDef HAL_FLASH_UnlockPages(uint32_t StartAddr, uint32_t EndAddr);
HAL_StatusTypeDef HAL_FLASH_ErasePages(uint32_t StartAddr, uint32_t EndAddr);
HAL_StatusTypeDef HAL_FLASH_WriteByte(uint32_t WriteAddr, uint8_t WrData);
HAL_StatusTypeDef HAL_FLASH_WriteBytes(uint32_t WriteAddr, uint8_t  *pWrBuf, uint32_t WrByteCnt);
HAL_StatusTypeDef HAL_FLASH_WriteWords(uint32_t WriteAddr, uint32_t *pWrBuf, uint32_t WrWordCnt);
HAL_StatusTypeDef HAL_OTP_WriteBytes(uint32_t WriteAddr, uint8_t  *pWrBuf, uint32_t  WrByteCnt);
uint8_t           HAL_FLASH_ReadByte(uint32_t ReadAddr);
void              HAL_FLASH_ReadBytes(uint32_t ReadAddr,  uint8_t  *pRdBuf, uint32_t RdByteCnt);


#ifdef __cplusplus
}
#endif

#endif /* __SYM32L010_HAL_FLASH_H */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
